package com.awspaass.user.apps.businesscar;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

@Controller
public class AppCmdBusinesscarDriver {
	/**
	 * 司机查询进行中的任务单
	 * 
	 * @param uc
	 * @param roleType
	 * @param bDate
	 * @param eDate
	 * @param page
	 * @param pageCount
	 * @param taskType
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.businesscar_driverGetMission")
	public String driverGetMission(UserContext uc, int roleType, String bDate, String eDate, int page, int pageCount,
			int taskType) {

		JSONObject returnData = new JSONObject();
		try {
			String userId = uc.getUID();
			String sid = uc.getSessionId();
			if (page < 1) {
				returnData.put("status", "1");
				returnData.put("message", "请传入大于等于1的起始页！");
				return returnData.toString();
			}
			// 获取起始条数和结束条数
			int startPage = (page - 1) * pageCount + 1;
			int endPage = page * pageCount;
			String timePeriod = "";// 行车确认是否查全部
			if (!bDate.equals("") && !eDate.equals("")) {
				timePeriod = "AND B.USEDATE >= TO_DATE('" + bDate + "','yyyy-MM-dd') AND B.USEDATE <= TO_DATE('" + eDate
						+ "','yyyy-MM-dd')";
			}
			String queryMissionInfoSql = "";

			queryMissionInfoSql = "SELECT * FROM (SELECT TT.*, ROWNUM AS rowno FROM (SELECT (CASE WHEN B.SJLXFS IS NOT NULL THEN CONCAT("
					+ "CONCAT(CONCAT(B.SJXM,'('),B.SJLXFS),')') ELSE CONCAT(B.SJXM,B.SJLXFS) END) SJXX,B.APPLYUSERNAME,B.APPLYUSERMOBILE,B.CPH,B.USEDATE,B.CFSJ,B.FHSJ,B.VEHICLETYPE,"
					+ "(CASE WHEN B.MISSIONSTATUS=0 THEN '未派单' WHEN B.MISSIONSTATUS=1 THEN '已派单' WHEN B.MISSIONSTATUS=2 THEN '已接单' WHEN B.MISSIONSTATUS"
					+ "=3 THEN '待结算' WHEN B.MISSIONSTATUS=4 THEN '待确认' WHEN B.MISSIONSTATUS=5 THEN '已确认' WHEN B.MISSIONSTATUS=6 THEN '已取消' ELSE '' END) MISSIONSTATUS,A.PROCESSINSTID,A.ID,'1' RWZT FROM BO_EU_YBBZUSECAR_MISSION B LEFT JOIN WFC_TASK A ON "
					+ "A.PROCESSINSTID = B.BINDID WHERE B.SJZH = '" + userId + "' " + timePeriod
					+ " AND B.MISSIONSTATUS IN (1,2,3) AND A.DISPATCHID IS NOT NULL AND A.TASKTITLE NOT LIKE '%空标题%'"
					+ " ORDER BY B.USEDATE) TT WHERE ROWNUM <= " + endPage + ") table_alias WHERE table_alias.rowno >= "
					+ startPage + "";

			
			System.out.println(queryMissionInfoSql);

			String portalUrl = SDK.getPortalAPI().getPortalUrl();// http://localhost:8088/portal
			JSONArray jsonMission = new JSONArray();

			if (!queryMissionInfoSql.equals("")) {
				List<Map<String, Object>> missonInfoList = DBSql.query(queryMissionInfoSql, new ColumnMapRowMapper(),
						new Object[] {});
				if (missonInfoList == null || missonInfoList.isEmpty()) {
					returnData.put("status", "0");
					returnData.put("jsonMisson", jsonMission);
				} else {
					for (int i = 0; i < missonInfoList.size(); i++) {
						JSONObject jsonMissionObj = new JSONObject();
						Map<String, Object> missionMap = missonInfoList.get(i);
						String useDate = CoreUtil.objToStr(missionMap.get("USEDATE"));
						String driverInfo = CoreUtil.objToStr(missionMap.get("SJXX"));// 司机姓名（联系方式）
						String startUseTime = CoreUtil.objToStr(missionMap.get("CFSJ"));// 出车时间
						String endUseTime = CoreUtil.objToStr(missionMap.get("FHSJ"));// 返回时间
						
						if (!startUseTime.equals("")) {
							startUseTime = startUseTime.substring(0, 16);
						}
						if (!endUseTime.equals("")) {
							endUseTime = endUseTime.substring(0, 16);
						}

						String carType = CoreUtil.objToStr(missionMap.get("VEHICLETYPE"));// 车型
						String missionStatus = CoreUtil.objToStr(missionMap.get("MISSIONSTATUS"));// 状态
						String processInstId = CoreUtil.objToStr(missionMap.get("PROCESSINSTID"));// 流程实例ID
						String taskInstId = CoreUtil.objToStr(missionMap.get("ID"));// 任务实例Id
						String rwzt = CoreUtil.objToStr(missionMap.get("RWZT"));// 1：待办|2：已办
						String applyUserName = CoreUtil.objToStr(missionMap.get("APPLYUSERNAME"));
						String applyPhone = CoreUtil.objToStr(missionMap.get("APPLYUSERMOBILE"));
						String url = portalUrl + "/r/w?sid=" + sid
								+ "&cmd=CLIENT_BPM_FORM_MAIN_PAGE_OPEN&processInstId=" + processInstId
								+ "&openState=1&taskInstId=" + taskInstId + "&displayToolbar=true";
						jsonMissionObj.put("driverInfo", driverInfo);
						jsonMissionObj.put("startUseTime", startUseTime);
						jsonMissionObj.put("endUseTime", endUseTime);

						carType = SDK.getDictAPI().getValue("com.awspaas.user.apps.shhtaerospaceindustrial", "shcartype", carType,
								"CNNAME");
						jsonMissionObj.put("carType", carType);
						jsonMissionObj.put("applyUserName", applyUserName);
						jsonMissionObj.put("applyPhone", applyPhone);
						jsonMissionObj.put("missionStatus", missionStatus);
						jsonMissionObj.put("rwzt", rwzt);
						jsonMissionObj.put("processInstId", processInstId);
						jsonMissionObj.put("url", url);
						jsonMissionObj.put("useDate", useDate);
						
						jsonMission.add(jsonMissionObj);
					}
				}
			}

			returnData.put("status", "0");
			returnData.put("jsonMission", jsonMission);
		} catch (Exception e) {
			e.printStackTrace();
			returnData.put("status", "1");
			returnData.put("message", e.getMessage());
		}

		return returnData.toString();

	}
}
